<?php

function _get($params = array(), $key = null) {
    if (isset($params[$key]) && !is_null($params[$key])) {
        return $params[$key];
    } else {
        return null;
    }
}

/**
 * Genere un lien
 * 
 * @param string $text en clair
 * @param string $path 
 * @param array $query_data 
 * @param array $attributs_data
 * @return string 
 */
function a($text, $path, $query_data = array(), $attributs_data = array()) {
    if (is_array($query_data) && count($query_data) > 0) {
        $path = $path . '?' . http_build_query($query_data);
    }

    $attributs = array();
    foreach ($attributs_data as $key => $value) {
        $attributs[] = "$key=\"$value\"";
    }
    $output = "<a href=\"$path\"  " . implode(" ", $attributs) . "  >  " . $text . "</a>";
    return $output;
}

function cmp($a, $b) {
    return ($a->id > $b->id) ? 1 : 0;
}

function t($text, $lg = null) {
    $db_local = Model_Local::getInstance();

    if ($lg == null) {
        return isset($db_local->data[$text][LANG]) ? $db_local->data[$text][LANG] : $text;
    } else {
        return isset($db_local->data[$text][$lg]) ? $db_local->data[$text][$lg] : $text;
    }
}

/**
 * Return le html pour generer une image
 * @param string $path nom de l'image
 * @param type $style (full (default), thumbnail,small)
 * @return HTML img 
 */
function img($path, $style = "original", $attributs_data = array()) {
    $path = $style . '/' . $path;
    return '<img src="' . FILEPATH . $path . '"/>';
}

function imghtml($path, $attributs_data = array()) {
    $attributs = array();
    foreach ($attributs_data as $key => $value) {
        $attributs[] = "$key=\"$value\"";
    }
    return '<img src="' . IMGPATH . $path . '"'.implode(" ", $attributs).'/>';
}

function dailyUpdate(){
    $db_total = Model_Total::getInstance();
    if(!isset($_SESSION['check'])){
        $yesterday = date("Y-m-d 00-00-00", time() - 86400);
        $stmt = $db_total->_db->query("SELECT date FROM total WHERE `date` = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)");
        $date = $stmt->fetch();
        $total = $db_total->_db->query("SELECT sum(total) as total FROM view_total WHERE `date` = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)");
        $result = $total->fetch();
        
        if(!is_object($date)){
            $db_total->insert(array('date'=>$yesterday,'total'=>$result->total));
        }else{

        }
    }else{
        
    }
    
}